import {
    request,
    AbstractDiscoverCrawler,
    DiscoverCrawlerTest,
    DiscoverBook,
    DiscoverSource,
    BookLink,
    PageResult,
} from "../../common/common_node.js";


class DiscoverCrawler extends AbstractDiscoverCrawler {

    async filterCategoryList(gender) {
        return [];
    }

    async bookPage(pageNum, gender, filterParameterList) {

        const url = this.source.portalUrl + "?page=" + pageNum;
        const $ = await (await request(url)).doc();

        const fictionList = $(".fiction-list");
        const bookArr = [];
        const that = this;
        fictionList.each(function (i, e) {
            const bookPath = $(e).find(".title a").attr("href");
            bookArr.push(new DiscoverBook({
                name: $(e).find(".title a").text().trim(),
                author: $(e).find(".author a").text().trim(),
                intro: $(e).find(".brief").text().trim(),
                link: new BookLink({
                    subscriptionId: that.source.subscriptionId,
                    sourceId: that.source.id,
                    bookPath: bookPath
                })
            }));
        })

        // 获取所有的页码链接
        const pageLinks = $(".pager a");
        let pages = 1; // 默认设置为1
        pageLinks.each(function () {
            const href = $(this).attr("href");
            if (!href) {
                return;
            }
            // 使用正则表达式从href中提取页码
            const regex = /page=(\d+)/;
            const match = href.match(regex);
            if (match) {
                pages = Math.max(pages, parseInt(match[1], 10));
            }
        });

        return new PageResult({
            pageNum: pageNum,
            pages: pages,
            list: bookArr
        });
    }
}

const source = {
    "id": "cnbook/1",
    "name": "xbook",
    "active": true,
    "portalUrl": "https://xbookcn.org",
    "description": null,
    "pageTurningMode": "pagination",
    "enableGenderFilter": false,
    "enableDiscover": true,
    "enableHotKeyword": false
};

await new DiscoverCrawlerTest(new DiscoverCrawler(DiscoverSource.createTestDiscoverSource(source))).run();
